package com.maixin.code.action;

import java.util.List;

import com.maixin.code.base.BaseAction;
import com.maixin.code.entity.Authority;
import com.maixin.code.entity.Role;
import com.maixin.code.entity.Roleauthority;
import com.maixin.code.service.AuthorityService;
import com.maixin.code.service.RoleAuthorityService;
import com.maixin.code.service.RoleService;
import com.maixin.code.store.ComboStore;
import com.maixin.code.store.TreeStore;
import com.maixin.code.utils.CommonValues;
import com.maixin.code.utils.Json2JavaUtils;

public class RoleAction  extends BaseAction{
	private static final long serialVersionUID = 1L;
	private String msg;
	private RoleService roleSer;
	private AuthorityService authoritySer;
	private RoleAuthorityService roleAuthoritySer;
	private List<Authority> list;
	private Role role;
	private Authority authority;
	private Roleauthority roleAuthority;
	private List<Role> rolelist;
	private List<Authority> authoritylist;
	private Integer rolecnt;
	private Integer authoritycnt;
	private Integer cnt;
	private List<TreeStore> treeList;
	private String dataStr;
	private List<ComboStore> comboList;
	
	public String findAllRoleAuth(){
		list = authoritySer.getRoleAuthDAO().findAllRoles(null);
		//cnt=150;
		return SUCCESS;
	}
	/**
	 * 角色列表
	 * @return
	 * @throws Exception 
	 */
	public String roleList () throws Exception{
		rolelist = roleSer.getBaseDao().findAll(Role.class, start, limit);	
		rolecnt = roleSer.getBaseDao().findTabCnt(Role.class);
		return SUCCESS;
	}
	/**
	 * 添加或修改角色
	 * TODO 需要处理角色下权限的问题,页面的CheckBoxTree
	 * @return
	 */
	public String addRole(){
		try{
			role = (Role) Json2JavaUtils.json2Java(content, Role.class).get(0);
			if(null == role.getId() || "".equals(role.getId())){
				List<Role> l = roleSer.getBaseDao().findByProperty("rolename", role.getRolename(), Role.class);
				if(l.size()>0){
					msg = CommonValues.CRUD_MSG[0];
				}else{
					role = roleSer.getBaseDao().save(role);
					String [] authorityArr = dataStr.split(",");
					for (int i = 0; i < authorityArr.length; i++) {
						if(!authorityArr[i].endsWith("_ALL")){
							roleAuthoritySer.getBaseDao().save(new Roleauthority(role.getId(), authorityArr[i].trim()));
						}
					}
				}
			}else{
				List<Role> l = roleSer.getBaseDao().findByProperty("rolename", role.getRolename(), Role.class);
				if(l!=null&&l.size()>1){
					msg = CommonValues.CRUD_MSG[0];
				}else{
					role = roleSer.getBaseDao().update(role);
					//先删除,后插入
					authoritySer.getRoleAuthDAO().delRoleAuth(role.getId());
					String [] authorityArr = dataStr.split(",");
					for (int i = 0; i < authorityArr.length; i++) {
						if(!authorityArr[i].endsWith("_ALL")){
							roleAuthoritySer.getBaseDao().save(new Roleauthority(role.getId(), authorityArr[i].trim()));
						}
					}
				}
				
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return SUCCESS;
	}
	
	/**
	 * 删除角色
	 * @return
	 * @throws Exception
	 */
	public String removeRoleAndAuthority() throws Exception{
		roleSer.getBaseDao().delete(role);
		//这里还需要删除角色下得权限
		authoritySer.delRoleAuthority(role.getId());
		return SUCCESS;
	}
	/**
	 * 权限列表
	 * @return
	 * @throws Exception 
	 */
	public String authorityList () throws Exception{
		authoritylist = authoritySer.getBaseDao().findAll(Authority.class, start, limit);	
		authoritycnt = authoritySer.getBaseDao().findTabCnt(Authority.class);
		return SUCCESS;
	}
	
	public String findParAuth(){
		comboList = authoritySer.getRoleAuthDAO().findParAuth();
		return SUCCESS;
	}
	
	/**
	 * 添加或修改权限
	 * @return
	 */
	public String addAuthority(){
		try{
			authority = (Authority) Json2JavaUtils.json2Java(content, Authority.class).get(0);
			if(null == authority.getId() || "".equals(authority.getId())){
				authority = authoritySer.getBaseDao().save(authority);
			}else{
				authority = authoritySer.getBaseDao().update(authority);
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return SUCCESS;
	}
	
	/**
	 * 删除单个权限
	 * @return
	 * @throws Exception
	 */
	public String removeAuthority() throws Exception{
		authoritySer.getBaseDao().delete(authority);
		return SUCCESS;
	}
	public String findRoleAuhorityStr(){
		dataStr = authoritySer.getAllByRole(role.getId());	
		return SUCCESS;
	}
	
	public String roleAuthTree(){
		treeList = authoritySer.getAllAuthRole();	
		return SUCCESS;
	}
	
	public RoleService getRoleSer() {
		return roleSer;
	}
	public void setRoleSer(RoleService roleSer) {
		this.roleSer = roleSer;
	}
	public AuthorityService getAuthoritySer() {
		return authoritySer;
	}
	public void setAuthoritySer(AuthorityService authoritySer) {
		this.authoritySer = authoritySer;
	}
	public RoleAuthorityService getRoleAuthoritySer() {
		return roleAuthoritySer;
	}
	public void setRoleAuthoritySer(RoleAuthorityService roleAuthoritySer) {
		this.roleAuthoritySer = roleAuthoritySer;
	}

	public List<Authority> getList() {
		return list;
	}

	public void setList(List<Authority> list) {
		this.list = list;
	}
	public Role getRole() {
		return role;
	}
	public void setRole(Role role) {
		this.role = role;
	}
	public Authority getAuthority() {
		return authority;
	}
	public void setAuthority(Authority authority) {
		this.authority = authority;
	}
	public List<Role> getRolelist() {
		return rolelist;
	}
	public void setRolelist(List<Role> rolelist) {
		this.rolelist = rolelist;
	}
	public List<Authority> getAuthoritylist() {
		return authoritylist;
	}
	public void setAuthoritylist(List<Authority> authoritylist) {
		this.authoritylist = authoritylist;
	}
	public Integer getRolecnt() {
		return rolecnt;
	}
	public void setRolecnt(Integer rolecnt) {
		this.rolecnt = rolecnt;
	}
	public Integer getAuthoritycnt() {
		return authoritycnt;
	}
	public void setAuthoritycnt(Integer authoritycnt) {
		this.authoritycnt = authoritycnt;
	}
	public String getMsg() {
		return msg;
	}
	public void setMsg(String msg) {
		this.msg = msg;
	}
	public String getDataStr() {
		return dataStr;
	}
	public void setDataStr(String dataStr) {
		this.dataStr = dataStr;
	}
	public Integer getCnt() {
		return cnt;
	}
	public void setCnt(Integer cnt) {
		this.cnt = cnt;
	}
	public List<TreeStore> getTreeList() {
		return treeList;
	}
	public void setTreeList(List<TreeStore> treeList) {
		this.treeList = treeList;
	}
	public Roleauthority getRoleAuthority() {
		return roleAuthority;
	}
	public void setRoleAuthority(Roleauthority roleAuthority) {
		this.roleAuthority = roleAuthority;
	}
	public List<ComboStore> getComboList() {
		return comboList;
	}
	public void setComboList(List<ComboStore> comboList) {
		this.comboList = comboList;
	}
	
}
